using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

using ScottPlot.Plottable;

using Lockzhiner;

namespace CS_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            // 新建DAQ122控制类
            daq122_ = new DAQ122();

            InitializeComponent();
            for (int i = 0; i < 8; i++)
            {
                plt_list[i] = formsPlot1.Plot.AddSignal(receive_data_[i], RECEIVE_BUFFER_SIZE);
                plt_list[i].LineColor = color_list[i];
                plt_list[i].IsVisible = true;
            }
            formsPlot1.Plot.SetAxisLimitsY(0, 5);
            formsPlot1.Refresh();
        }
        private void button1_Click_1(object sender, EventArgs e)
        {
            // 有Create就要有DeleteDevice，否则可能造成内存泄露
            daq122_.DeleteDevice();
            daq122_.CreateDevice();
            // 初始化DAQ122设备
            var result = daq122_.InitializeDevice();
            if (result == false)
            {
                Console.WriteLine("DAQ122设备初始化失败");
                return;
            }
            Console.WriteLine("初始化设备成功");

            // 连接DAQ122设备
            result = daq122_.ConnectedDevice();
            if (result == false)
            {
                Console.WriteLine("连接DAQ122设备失败");
                return;
            }
            Console.WriteLine("连接设备成功");

            //  配置DAQ122采样率
            var config_result = daq122_.ConfigureADCSamplingParameters(VOLTAGE_RANGE, SAMPLE_RATE);
            if (config_result == false)
            {
                Console.WriteLine("DAQ122配置电压失败");
                return;
            }
            Console.WriteLine("DAQ122配置电压成功");

            // 配置使用哪些ADC通道
            daq122_.ConfigADCChannel(OPEN_CHANNEL);
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            // 清空数据缓冲区
            daq122_.ClearDataBuffer();
            var satrt_result = daq122_.StartADCCollection();
            if (satrt_result == false)
            {
                Console.WriteLine("satrt_result is {0}", satrt_result);
                return;
            }
            read_data_timer.Start();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            read_data_timer.Stop();
            daq122_.StopADCCollection();
        }

        private void read_data_timer_Tick(object sender, EventArgs e)
        {
            // 判断当前缓冲区是否符合读取的要求
            if (!daq122_.ADCDataBufferIsValid(RECEIVE_BUFFER_SIZE) ) {
                return;
            }

            // 读取数据到缓冲区
            for (int channel_index = 0; channel_index < 8; channel_index++) {
                // 如果未选择通道则直接跳过
                if ((OPEN_CHANNEL & (0b00000001 << channel_index)) == 0)
                {
                    continue;
                }

                // 读取数据
                bool read_result = daq122_.TryReadData((UInt32)channel_index, ref receive_data_[channel_index][0], RECEIVE_BUFFER_SIZE, 250);
                if (!read_result)
                {
                    Console.WriteLine("数据读取失败");
                    return;
                }
            }

            // 刷新显示控件
            formsPlot1.Refresh();
        }

        private DAQ122 daq122_;

        // 配置采样率为200K
        private static DAQSampleRate SAMPLE_RATE = DAQSampleRate.SampleRate200K;

        // 配置电压量程为5V
        private static DAQVoltage VOLTAGE_RANGE = DAQVoltage.Voltage5V;

        // 配置采集只采集前4个通道
        private static UInt32 OPEN_CHANNEL = (UInt32)(DAQADCChannel.AIN1 | DAQADCChannel.AIN2 | DAQADCChannel.AIN3 | DAQADCChannel.AIN4);
        // 配置采集8个通道
        //private static UInt32 OPEN_CHANNEL = (UInt32)(DAQADCChannel.AIN_ALL);

        // 配置8个通道
        SignalPlot[] plt_list = new SignalPlot[8];
        // 配置8个通道的颜色
        Color[] color_list = {
            Color.Black , Color.Red, Color.Blue , Color.DarkGreen ,
            Color.Turquoise,Color.Magenta ,Color.SaddleBrown,Color.Orange
        };

        // 设置缓冲区大小为采样率大小
        private static UInt32 RECEIVE_BUFFER_SIZE = (UInt32)SAMPLE_RATE;
        // 设置缓冲区
        double[][] receive_data_ = {
            new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE],
            new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE]
        };

        public delegate void RefreshFomrsplot();
    }
}
